<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\user_center\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;

/**
 * MCN机构 艺人管理
 * @package app\user_center\admin
 */
class Artist extends Admin
{
    /**
     * 首页
     * @author jason wang <120907037@qq.com>
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        
        // 获取筛选
        $map = $this->getMap();//dump($map);die;

        //数据获取
        $data = input();
        $attestation_id = isset($data['attestation_id']) ? $data['attestation_id'] : 0;
        if($attestation_id > 0) $map[] = ['attestation_id', '=', $attestation_id];

        // 数据列表
        $data_list = model('UcAttestationArtist', 'model\mcn') -> where($map) -> order('create_time desc') -> paginate();
        $rel_list = [];
        foreach ($data_list as $key => $value) {
            $rel_list[$key] = $value;
            $rel_list[$key]['attestation_name'] = $value['get_uc_attestation']['attestation_name'];
            $rel_list[$key]['class_title'] = $value['get_video_play_class']['title'];
            $rel_list[$key]['mobile'] = $value['get_uc_member_mobile']['mobile'];
        }
        //查询分类信息
    

        //dump($data_list);die;
        // 添加艺人
        $btn_add_artist = [
            'title' => '添加',
            'icon'  => 'fa fa-plus-circle',
            'class' => 'btn-primary btn',
            'href'  => url('add', ['attestation_id' => $attestation_id])
        ];
        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('艺人管理') // 设置页面标题
            ->setPrimaryKey('id') // 设置主键名为username
            ->setTableName('uc_attestation_artist') // 设置数据表名
            ->setSearch(['id' => '艺人ID', 'artist_name' => '艺人名称', 'uid' => '会员UID']) // 设置搜索参数
            ->addColumns([ // 批量添加列
                ['__INDEX__', '序号'],
                ['id', '艺人ID'],
                ['attestation_name', '所属机构'],
                ['class_title', '类别'],
                ['artist_name', '艺人名称', 'text.edit'],
                ['mobile', '手机号'],
                //['artist_times', '艺人作品数'],
                ['create_time', '创建时间', 'datetime'],
                ['artist_status', '状态', 'switch'],
                ['right_button', '操作', 'btn']
            ])
            ->addTimeFilter('create_time', '', ['开始时间', '结束时间'])
            ->addTopButton('custom', $btn_add_artist, true) // 添加授权按钮
            ->addTopButtons('enable,disable,delete') // 批量添加顶部按钮
            ->addRightButtons('delete') // 批量添加右侧按钮
            ->setRowList($rel_list) // 设置表格数据
            ->fetch(); // 渲染页面
    }

    /**
     * 新增
     * @author jason wang <120907037@qq.com>
     * @return mixed
     * @throws \think\Exception
     */
    public function add(){   
            //model('UcAttestationArtist', 'model\mcn')::startTrans();//启动事务
            // 保存数据
            if ($this->request->isPost()) {
                //数据接收
                $data = $this->request->post();
                $mobile         = isset($data['mobile']) ? $data['mobile'] : '';
                $artist_name    = isset($data['artist_name']) ? $data['artist_name'] : '';
                $artist_status  = isset($data['artist_status']) ? $data['artist_status'] : '';
                $attestation_id  = isset($data['attestation_id']) ? $data['attestation_id'] : '';
                $classid         = isset($data['classid']) ? $data['classid'] : '';

                //数据验证
                $Validate = validate('Artist', 'validate\admin');
                if( !$Validate -> scene('add') -> check($data) ){
                    $this->error($Validate -> getError());
                }
                
                $uid = model('UcAttestation', 'model\mcn') -> find($attestation_id)->uid;
                //业务逻辑 
                $rel = controller('Mcnlogic','logic\mcn') -> add_artist($uid, $mobile, $artist_name, $artist_status, $classid);

                if($rel['code'] == 200){
                    //操作行为
                    //Hook::listen('user_add', $user);
                    // 记录行为
                    //action_log('artist_add', 'admin_user', $user['id'], UID);
                    //model('UcAttestationArtist', 'model\mcn')::commit();
                    $this->success('新增成功', '', '_parent_reload');
                }else{
                    $this->error($rel['message']);
                }

            }
         

        //数据接收
        $attestation_id = input('attestation_id');
        //查询机构
        $list_attestation = model('UcAttestation', 'model\mcn') -> select();
        $list_attestation_rel = [];
        foreach ($list_attestation as $key => $value) {
            $list_attestation_rel[$value['id']] = $value['attestation_name'];
        }
        //查询分类信息
        $map = [];
        $map['pid'] = 0;
        $list_class = model('video/VideoPlayClass') ->where($map) -> order('sort asc,id asc') -> select();
        foreach ($list_class as $key => $value) {
            $list_class_arr[$value['id']] = $value['title'];
        }

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('新增') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['select', 'attestation_id', '所属机构', '请选择机构', $list_attestation_rel, $attestation_id],
                ['select', 'classid', '类别', '请选择类别',$list_class_arr],
                ['text', 'artist_name', '创作者名称', '必填，可由英文字母、数字组成'],
                ['text', 'mobile', '手机号', '必填，必须是已绑定的用户'],
                ['radio', 'artist_status', '状态', '', ['禁用', '启用'], 1],
            ])
            ->fetch();
    }

    /**
     * 编辑
     * @param null $id 用户id
     * @author jason wang <120907037@qq.com>
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit($id = null)
    {
        if ($id === null) $this->error('缺少参数');

        //数据接收
        $attestation_id = $id;
        //查询机构
        $list_attestation = model('UcAttestation', 'model\mcn') -> select();
        foreach ($list_attestation as $key => $value) {
            $list_attestation_rel[$value['id']] = $value['name'];
        }
        //查询分类信息
        $list_class_arr = [];
        $map = [];
        $map['pid'] = 0;
        $list_class = model('video/VideoPlayClass') ->where($map) -> order('sort asc,id asc') -> select();
        foreach ($list_class as $key => $value) {
            $list_class_arr[$value['id']] = $value['title'];
        }

        //信息读取
        $info = model('UcAttestationArtist', 'model\mcn') -> find($attestation_id);;

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('编辑') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['select', 'attestation_id', '所属机构', '请选择机构', $list_attestation_rel, $attestation_id],
                //['select', 'classid', '类别', '请选择类别',$list_class_arr],
                ['text', 'artist_name', '艺人名称', '必填，可由英文字母、数字组成'],
                //['text', 'mobile', '手机号', '必填，必须是已绑定的用户'],
                ['radio', 'artist_status', '状态', '', ['禁用', '启用'], 1],
            ])
            ->setFormData($info) // 设置表单数据
            ->fetch();
    }

    /**
     * 删除 艺人
     * @param array $ids 用户id
     * @author jason wang <120907037@qq.com>
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delete($ids = [])
    {
        //删除数据
        $rel = model('UcAttestationArtist', 'model\mcn') -> destroy($ids);
        if($rel){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }

    /**
     * 启用用户
     * @param array $ids 用户id
     * @author jason wang <120907037@qq.com>
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function enable($ids = [])
    {
        $rel = model('UcAttestationArtist', 'model\mcn') -> where('id', 'in', $ids)  -> setField('artist_status', 1);
        if($rel){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }

    /**
     * 禁用用户
     * @param array $ids 用户id
     * @author jason wang <120907037@qq.com>
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function disable($ids = [])
    {
        $rel = model('UcAttestationArtist', 'model\mcn') -> where('id', 'in', $ids)  -> setField('artist_status', 0);
        if($rel){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }

    

    
}
